﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Web;

namespace WcfAdvice.AOP
{
    public class TokenInspectorBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            Trace.WriteLine("AddBindingParameters");
            return;
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 可以在客户端应用程序中修改、检查或插入对终结点中的扩展
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="clientRuntime"></param>
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            Trace.WriteLine("ApplyClientBehavior");
            clientRuntime.ClientMessageInspectors.Add(new TokenClientInspector());
        }

        /// <summary>
        /// 可以在服务应用程序中修改、检查或插入对终结点范围执行的扩展
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="endpointDispatcher"></param>
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            Trace.WriteLine("ApplyDispatchBehavior");
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new TokenServiceInspector());
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            Trace.WriteLine("Validate");
            return;
        }
    }
}